package com.nus.database.util;

import com.nus.beans.*;
import com.nus.services.ConversionService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/26/12
 * Time: 11:44 PM
 * To change this template use File | Settings | File Templates.
 */
public class DataReadUtils {
    public static User getUserInformation(ResultSet resultSet) throws SQLException {
        User user = new User();
        user.setLoginName(resultSet.getString("LOGINNAME"));
        user.setPassword(resultSet.getString("PASSWORD"));
        user.setFirstName(resultSet.getString("FIRSTNAME"));
        int dbUserType = Integer.parseInt(resultSet.getString("USERTYPE"));

        ConversionService conversionService = new ConversionService();
        UserType userType = conversionService.getUserType(dbUserType);
        user.setUserType(userType);

        int dbUserStatus = resultSet.getInt("userstatus");
        UserStatus userStatus = conversionService.getUserStatus(dbUserStatus);
        user.setStatus(userStatus);

        user.setLastName(resultSet.getString("LASTNAME"));
        user.setAddress(resultSet.getString("ADDRESS"));
        user.setPhoneNumber(resultSet.getLong("PHONENO"));
        user.setDateOfBirth(resultSet.getDate("DATEOFBIRTH"));
        user.setBasicSalary(resultSet.getLong("BASICSALARY"));
        user.setRemainingLeaves(resultSet.getInt("REMAININGLEAVES"));
        user.setUserId(resultSet.getInt("ID"));
        user.setFailedLoginAttempts(resultSet.getInt("FAILED_LOGIN_ATTEMPTS"));
        Timestamp timestamp = resultSet.getTimestamp("LAST_FAILED_LOGIN_TIME");
        if (timestamp != null) {
            user.setLastLoginFailedTime(new Date(timestamp.getTime()));
        }

        int dbUserSubType = resultSet.getInt("usersubtype");
        UserSubType userSubType = conversionService.getUserSubType(dbUserSubType);
        user.setUserSubType(userSubType);
        return user;
    }

    public static Project getProjectInformation(ResultSet resultSet) throws SQLException {
        Project project = new Project();
        project.setId(resultSet.getInt("ID"));
        project.setProjectName(resultSet.getString("NAME"));
        project.setStartingDate(resultSet.getDate("STARTDATE"));
        project.setEstimatedEndDate(resultSet.getDate("ESTIMATEDENDDATE"));
        project.setDeadline(resultSet.getDate("DEADLINE"));

        int dbStatusId = Integer.parseInt(resultSet.getString("STATUSID"));

        ConversionService conversionService = new ConversionService();
        ProjectStatus projectStatus = conversionService.getProjectStatus(dbStatusId);
        project.setStatus(projectStatus);
        return project;
    }

    public static ProjectRemark getProjectRemark(ResultSet resultSet) throws SQLException {
        ProjectRemark projectRemark = new ProjectRemark();
        projectRemark.setRemark(resultSet.getString("DATA"));
        User user = getUserInformation(resultSet);
        projectRemark.setEnteredBy(user);
        projectRemark.setId(resultSet.getLong("ID"));
        projectRemark.setProjectId(resultSet.getInt("projectid"));
        return projectRemark;
    }

    public static UserEvaluation getUserEvaluation(ResultSet resultSet) throws SQLException {
        UserEvaluation userEvaluation = new UserEvaluation();
        userEvaluation.setId(resultSet.getInt("ID"));

        User user = DataReadUtils.getUserInformation(resultSet);
        userEvaluation.setEnteredBy(user);
        userEvaluation.setUserId(resultSet.getInt("USER_ID"));
        userEvaluation.setEvaluation(resultSet.getString("EVALUATION"));
        return userEvaluation;
    }
}
